<?php

namespace app\api\service;


use app\api\model\Push as PushModel;
use app\api\model\User as UserModel;
use JPush\Client;
use think\Log;

class Push
{
    const APP_KEY = '54a6b7108a55aceb40ac981d';
    const MASTER_SECRET = '8d33b705d1c5edab3700d31f';

    //添加设备,极光推送ID信息

    //推送
    public static function push($registration_id, $alert)
    {
        import('jpush.autoload', EXTEND_PATH, '.php');
        $client = new Client(self::APP_KEY, self::MASTER_SECRET);
        $res = $client->push()
            ->setPlatform('all')
            ->addRegistrationId([$registration_id])
            ->setNotificationAlert($alert);
        try {
            $res->send();
        } catch (\JPush\Exceptions\JPushException $e) {
            // try something else here
//            print $e->getMessage();
            Log::write($e->getMessage(), 'push ');
        }
//        dump($res);
    }

    //收益推送
    public static function income($user_id, $info)
    {
        $reg_id = UserModel::where('id', $user_id)->value('reg_id');
        import('jpush.autoload', EXTEND_PATH, '.php');
        //记录推送日志
        $push_data = [
            'user_id' => $user_id,
//            'reg_id' => $reg_id,
            'title' => '奖励到账提醒',
            'alert' => $info['alert'],
            'add_time' => time()
        ];
        PushModel::create($push_data);
//        $client = new Client(self::APP_KEY, self::MASTER_SECRET);
//        $res = $client->push()
//            ->setPlatform('all')
//            ->addRegistrationId([$reg_id])
//            ->setNotificationAlert($info['alert']);
//        try {
//            $res->send();
//        } catch (\JPush\Exceptions\JPushException $e) {
//            // try something else here
////            print $e;
//            Log::write($e->getMessage() . 'reg_id:' . $reg_id, 'push_err_'.$user_id);
//        }
    }

}